package subscriber.fail;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

import com.prosyst.util.ref.Log;

public class Activator implements BundleActivator, EventHandler {

	private BundleContext bundleContext;
	private ServiceRegistration failEventHandlerServiceReg;
	Log log;

	private static final String FAIL_EVENT_TOPIC_VALUE = "libraryservice/search/event/fail";

	public void start(BundleContext context) throws Exception {
		this.bundleContext = context;

		initializeLog();

		Dictionary failEventHandlerPropertyMap = new Hashtable();
		failEventHandlerPropertyMap.put(EventConstants.EVENT_TOPIC, FAIL_EVENT_TOPIC_VALUE);
		failEventHandlerServiceReg = bundleContext.registerService(
				EventHandler.class.getName(), this, failEventHandlerPropertyMap);

	}

	public void stop(BundleContext context) throws Exception {
		log.close();
		log = null;

		failEventHandlerServiceReg.unregister();
		failEventHandlerServiceReg = null;
		this.bundleContext = null;
	}

	public void handleEvent(Event event) {
		log.info("[FAIL - EventHandler] Event property NOT_FOUND_BOOK: "
				+ event.getProperty("NOT_FOUND_BOOK"));

		log.info("[FAIL - EventHandler] Event property COMMON_COUNTER: "
				+ event.getProperty("COMMON_COUNTER"));
	}

	public void initializeLog() {
		log = new Log(bundleContext);
		log.setLogLevel(Log.INFO);
		log.setPrintLevel(Log.INFO);
	}

}
